﻿using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;

namespace Common.Standard
{
    public class ModelHelper
    {
        /// <summary>
        /// 模型转换
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static T ConvertToNewObject<T>(object obj) where T : new()
        {
            var sourceProperties = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);

            var resolveType = typeof(T);

            var instance = Activator.CreateInstance(resolveType);

            foreach (var sourcePropertity in sourceProperties)
            {
                PropertyInfo resolvePropertyInfo = resolveType.GetProperty(sourcePropertity.Name);
                object value = sourcePropertity.GetValue(obj, null);
                if (resolvePropertyInfo != null && value != null)
                {
                    resolvePropertyInfo.SetValue(instance, value, null);
                }
            }
            return (T)instance;
        }
        /// <summary>
        /// 将对象转换成字典,对象要求没有子对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public static Dictionary<string, string> ConvertToDictionary<T>(T t) where T : class
        {
            var dic = new Dictionary<string, string>();
            if (t != null)
            {
                PropertyInfo[] propertys = t.GetType().GetProperties();

                foreach (var pi in propertys)
                {
                    var key = pi.Name;
                    if (pi.GetValue(t) == null)
                    {
                        continue;
                    }
                    var value = pi.GetValue(t).ToString();
                    if (string.IsNullOrWhiteSpace(value))
                    {
                        continue;
                    }
                    dic.Add(key, value);
                }
            }
            return dic;
        }
        /// <summary>
        /// 将对象转换成链接参数 &
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public static string ConvertToUrlParameter<T>(T t) where T : class
        {
            var str = "";
            if (t != null)
            {
                PropertyInfo[] propertys = t.GetType().GetProperties().OrderBy(x => x.Name).ToArray();

                foreach (var pi in propertys)
                {
                    var key = pi.Name;
                    if (pi.GetValue(t) == null)
                    {
                        continue;
                    }
                    var value = pi.GetValue(t).ToString();
                    if (string.IsNullOrWhiteSpace(value))
                    {
                        continue;
                    }
                    str += $"{key}={value}&";
                }
            }
            return str.Trim('&');
        }
        /// <summary>
        /// url参数转字典
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public static Dictionary<string, string> UrlParameterToDictionary(string param)
        {
            if (string.IsNullOrWhiteSpace(param))
            {
                return null;
            }
            //去掉问号
            var p = param.TrimStart('?');
            //双连接符转单连接符
            p = p.Replace("&&", "&");
            var array = p.Split('&');
            if (array.Count() == 0)
            {
                return null;
            }
            var dic = new Dictionary<string, string>();
            foreach (var item in array)
            {
                var key = "";
                var value = "";
                var str = item.ToString().Split('=');
                if (str.Count() == 0)
                {
                    continue;
                }
                key = str[0].ToString();
                if (str.Count() == 2)
                {
                    value = str[1].ToString();
                }
                dic.Add(key, value);
            }
            return dic;
        }
        /// <summary>
        /// 将post过来的json序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="context"></param>
        /// <returns></returns>
        public static T PostParameterToModel<T>(HttpContext context) where T : class
        {
            if (context.Request.Method.ToUpper() == "POST")
            {
                using (Stream stream = context.Request.Body)
                {
                    byte[] bytes = new byte[context.Request.ContentLength.Value];
                    stream.Read(bytes, 0, bytes.Length);
                    stream.Dispose();

                    string postString = Encoding.UTF8.GetString(bytes);
                    if (!string.IsNullOrEmpty(postString))
                    {
                        return JsonHelper.JsonToModel<T>(postString);
                    }
                }
            }
            return null;
        }
    }
}
